/**
 * prototype Tools
 * ==============================================================================
 */
/**
 * 日期格式化
 * @param fmt
 * @returns {*}
 * @constructor
 */
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

/**
 * 格式化金钱
 * @returns {string}
 * @constructor
 */
Number.prototype.Money = function () {
    var value = this.valueOf();
    value += '';
    if (!value) return '0.00';

    /*原来用的是Number(value).toFixed(0)，这样取整时有问题，例如0.51取整之后为1*/
    var intPart = Number(value) | 0; // 获取整数部分
    var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); // 将整数部分逢三一断

    var floatPart = ".00"; // 预定义小数部分
    var value2Array = value.split(".");

    // =2表示数据有小数位
    if (value2Array.length == 2) {
        floatPart = value2Array[1].toString(); // 拿到小数部分

        if (floatPart.length == 1) { // 补0,实际上用不着
            return intPartFormat + "." + floatPart + '0';
        } else {
            return intPartFormat + "." + floatPart;
        }

    } else {
        return intPartFormat + floatPart;
    }
}

/**
 * 数字省略格式化
 * @returns {*}
 * @constructor
 */
Number.prototype.Omit = function () {
    var value = this.valueOf();
    if (!value) return 0;
    if (value > 9999) {
        return ('' + value).substring(0, ('' + value).length - 4) + '.' + ('' + value).substring(('' + value).length - 4, ('' + value).length - 3) + 'W';
    } else {
        return value;
    }
}

/**
 * Jquery Tools
 */
$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

$.fn.serializeObjectNoNullAttr = function () {
    var data = this.serializeObject();
    for (var p in data) {
        if (data[p] == null || data[p] == '') {
            delete data[p];
        }
    }
    return data;
};